// pages/test-count/test-count.js
var util = require('../../utils/util.js')
const DB = wx.cloud.database()
const $ = DB.command.aggregate
const _ = DB.command
//获取应用实例
const app = getApp()

let skip = 10

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //处理导 航 栏的高度
    rpxdata: wx.getStorageSync('rpxdata'),
    navigationBarAndStatusBarHeight: wx.getStorageSync('statusBarHeight') + wx.getStorageSync('navigationBarHeight') + 'px',
    navigationBarAndStatusBarAndsearchHeight: wx.getStorageSync('statusBarHeight') + wx.getStorageSync('navigationBarHeight') + 60 + 'px',
    //设置导航栏的显示信息
    navbarData: {
      showCapsule: 1,
      title: "按借阅人分类"
    },

    DBget:{}


  },
//计算借阅天数信息
  jieyuedate: function(startime) {
    //日期格式化
    var start_date = new Date(startime.replace(/-/g, "/"));
    var end_date = new Date();
    //转成毫秒数，两个日期相减
    var ms = end_date.getTime() - start_date.getTime();
    //转换成天数
    var day = parseInt(Math.ceil(ms / (1000 * 60 * 60 * 24)));
    //do something
    return day    
  } ,
//点击显示借书信息
  getinfo(e){
    let that = this
    let jyren = e.currentTarget.dataset.jyren
    let idx = e.currentTarget.dataset.idx
    let datatmp = this.data.DBget
    let dbtmp = []
    DB.collection("lchbook").orderBy('jytime','asc').where({jyren:jyren}).get({success:(res)=>{
      for (let i=0;i<res.data.length;i++){ 
        var obj = {}
        obj.bookname = res.data[i].bookname
        obj.jyren = res.data[i].jyren
        obj.jydate = res.data[i].jydate
        obj.isbn = res.data[i].isbn
        obj.day = that.jieyuedate(res.data[i].jytime)
        obj._id = res.data[i]._id
        dbtmp.push(obj) }
       datatmp[idx].data = dbtmp
     } 
     })
     that.setData({DBget:datatmp})


  },
//点击清除借书信息
clearinfo(e){
  let that = this
  let idx = e.currentTarget.dataset.idx
  let datatmp = this.data.DBget
  datatmp[idx].data = []
  this.setData({DBget:datatmp})

},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    DB.collection("lchbook").aggregate().match({jystate:true}).group({_id: '$jyren',count: $.sum(1)}).end({success:function(res){that.setData({DBget:res.list})  }})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})